package cn.com.cennavi.factory;

import java.util.Date;
import azkaban.utils.Props;
import cn.com.cennavi.bean.OdModelBean;
import cn.com.cennavi.interfaces.IMineLabModel;
import cn.com.cennavi.services.ODAnalysis;

/**
 * 
 * @ClassName: MineLabServiceFactory
 * @Description: TODO(minelab设计工厂类)
 * @author buf
 * @date 2017年2月8日 下午1:29:59
 *
 */
public class MineLabServiceFactory {

	/**
	 * azkaban固定写法
	 */
	private final String[] parameters;

	public MineLabServiceFactory(String name, Props props) throws Exception {
		this.parameters = props.getString("parameters").split(",");
	}

	public MineLabServiceFactory() {
		// TODO Auto-generated constructor stub
		this.parameters = null;
	}
	public void run() throws Exception {
		System.out.println("----------------hahaha---------------");
		System.out.println("userId:" + parameters[0]);
		System.out.println("modelId:" + parameters[1]);
		System.out.println("modelminId:" + parameters[2]);
		System.out.println("dataSourse:" + parameters[3]);
		System.out.println("dataClass:" + parameters[4]);
		System.out.println("startDateTime:" + parameters[5]);
		System.out.println("endDateTime:" + parameters[6]);
		System.out.println("spatialFlag:" + parameters[7]);
		System.out.println("meshCode:" + parameters[8]);
		System.out.println("countyCode:" + parameters[9]);
		System.out.println("cityCode:" + parameters[10]);
		System.out.println("provinceCode:" + parameters[11]);
		System.out.println("timegranularityFlag:" + parameters[12]);
		System.out.println("timegranularity:" + parameters[13]);
		System.out.println("spatialgranularityFlag:" + parameters[14]);
		System.out.println("geoshapePath:" + parameters[15]);
		System.out.println("outflag:" + parameters[16]);
		System.out.println("outPath:" + parameters[17]);
		System.out.println("taskId:" + parameters[18]);
		System.out.println("----------------hahaha---------------");
		String userId = parameters[0];
		String modelId = parameters[1];
		String modelminId = parameters[2];
		String dataSourse = parameters[3];
		String dataClass = parameters[4];
		String startDateTime = parameters[5];
		String endDateTime = parameters[6];
		int spatialFlag = Integer.parseInt(parameters[7]);
		int meshCode = Integer.parseInt(parameters[8]);
		int countyCode = Integer.parseInt(parameters[9]);
		int cityCode = Integer.parseInt(parameters[10]);
		int provinceCode = Integer.parseInt(parameters[11]);
		int timegranularityFlag = Integer.parseInt(parameters[12]);
		float timegranularity = Float.parseFloat(parameters[13]);
		int spatialgranularityFlag = Integer.parseInt(parameters[14]);
		String geoshapePath = parameters[15];
		int outflag = Integer.parseInt(parameters[16]);
		String outPath = parameters[17];
		String taskId = parameters[18];
		MineLabServiceFactory factory = new MineLabServiceFactory();
		IMineLabModel model = factory.getService(userId, modelId, modelminId, dataSourse, dataClass, startDateTime, endDateTime, spatialFlag, meshCode, countyCode, cityCode, provinceCode, timegranularityFlag, timegranularity, spatialgranularityFlag, geoshapePath, outflag, outPath, taskId);
		model.doJob();
	}
	public IMineLabModel getService(String userId, String modelId, String modelminId, String dataSourse, String dataClass, String startDateTime, String endDateTime, int spatialFlag, int meshCode, int countyCode, int cityCode, int provinceCode, int timegranularityFlag, float timegranularity, int spatialgranularityFlag, String geoshapePath, int outflag, String outPath, String tsakID) {
		switch (modelId) {
		// 1代表od模块
		case "odModel":
			return new ODAnalysis(userId, modelId, modelminId, dataSourse, dataClass, startDateTime, endDateTime, spatialFlag, meshCode, countyCode, cityCode, provinceCode, timegranularityFlag, timegranularity, spatialgranularityFlag, geoshapePath, outflag, outPath, tsakID);
		default:
			break;
		}
		return null;

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(args[0]);
		String[] arg1 = args[0].split(",");

		System.out.println("----------------hahaha---------------");
		System.out.println("userId:" + arg1[0]);
		System.out.println("modelId:" + arg1[1]);
		System.out.println("modelminId:" + arg1[2]);
		System.out.println("dataSourse:" + arg1[3]);
		System.out.println("dataClass:" + arg1[4]);
		System.out.println("startDateTime:" + arg1[5]);
		System.out.println("endDateTime:" + arg1[6]);
		System.out.println("spatialFlag:" + arg1[7]);
		System.out.println("meshCode:" + arg1[8]);
		System.out.println("countyCode:" + arg1[9]);
		System.out.println("cityCode:" + arg1[10]);
		System.out.println("provinceCode:" + arg1[11]);
		System.out.println("timegranularityFlag:" + arg1[12]);
		System.out.println("timegranularity:" + arg1[13]);
		System.out.println("spatialgranularityFlag:" + arg1[14]);
		System.out.println("geoshapePath:" + arg1[15]);
		System.out.println("outflag:" + arg1[16]);
		System.out.println("outPath:" + arg1[17]);
		System.out.println("taskId:" + arg1[18]);
		System.out.println("----------------hahaha---------------");
		String userId = arg1[0];
		String modelId = arg1[1];
		String modelminId = arg1[2];
		String dataSourse = arg1[3];
		String dataClass = arg1[4];
		String startDateTime = arg1[5];
		String endDateTime = arg1[6];
		int spatialFlag = Integer.parseInt(arg1[7]);
		int meshCode = Integer.parseInt(arg1[8]);
		int countyCode = Integer.parseInt(arg1[9]);
		int cityCode = Integer.parseInt(arg1[10]);
		int provinceCode = Integer.parseInt(arg1[11]);
		int timegranularityFlag = Integer.parseInt(arg1[12]);
		float timegranularity = Float.parseFloat(arg1[13]);
		int spatialgranularityFlag = Integer.parseInt(arg1[14]);
		String geoshapePath = arg1[15];
		int outflag = Integer.parseInt(arg1[16]);
		String outPath = arg1[17];
		String taskId = arg1[18];
		MineLabServiceFactory factory = new MineLabServiceFactory();
		IMineLabModel model = factory.getService(userId, modelId, modelminId, dataSourse, dataClass, startDateTime, endDateTime, spatialFlag, meshCode, countyCode, cityCode, provinceCode, timegranularityFlag, timegranularity, spatialgranularityFlag, geoshapePath, outflag, outPath, taskId);
		model.doJob();
	}

	public static OdModelBean getService(String args) {
		String[] arg1 = args.split(",");
		String userId = arg1[0];
		String modelId = arg1[1];
		String modelminId = arg1[2];
		String dataSourse = arg1[3];
		String dataClass = arg1[4];
		String startDateTime = arg1[5];
		String endDateTime = arg1[6];
		int spatialFlag = Integer.parseInt(arg1[7]);
		int meshCode = Integer.parseInt(arg1[8]);
		int countyCode = Integer.parseInt(arg1[9]);
		int cityCode = Integer.parseInt(arg1[10]);
		int provinceCode = Integer.parseInt(arg1[11]);
		int timegranularityFlag = Integer.parseInt(arg1[12]);
		float timegranularity = Float.parseFloat(arg1[13]);
		int spatialgranularityFlag = Integer.parseInt(arg1[14]);
		String geoshapePath = arg1[15];
		int outflag = Integer.parseInt(arg1[16]);
		String taskId = getTaskId(userId, modelId, modelminId, dataSourse, dataClass, startDateTime, endDateTime);
		String outPath = "";
		switch (outflag) {
		case 1:
			// 输出到kafka，outFlagAndPath为topic
			outPath = "kafka://" + taskId;
			break;
		case 2:
			// 输出到hdfs
			outPath = "hdfs://nujhadoop/test/minlab/od/" + taskId;
			break;
		case 3:
			// 到mysql
			outPath = "mysql://" + taskId;
			break;
		default:
			break;
		}
		MineLabServiceFactory factory = new MineLabServiceFactory();
		IMineLabModel model = factory.getService(userId, modelId, modelminId, dataSourse, dataClass, startDateTime, endDateTime, spatialFlag, meshCode, countyCode, cityCode, provinceCode, timegranularityFlag, timegranularity, spatialgranularityFlag, geoshapePath, outflag, taskId, outPath);
		model.doJob();
		OdModelBean data = new OdModelBean();
		data.userId = userId;
		data.modelId = modelId;
		data.modelminId = modelminId;
		data.dataSourse = dataSourse;
		data.dataClass = dataClass;
		data.startDateTime = startDateTime;
		data.endDateTime = endDateTime;
		data.spatialFlag = spatialFlag;
		data.meshId = meshCode;
		data.countyId = countyCode;
		data.cityCode = cityCode;
		data.provinceCode = provinceCode;
		data.timegranularityFlag = timegranularityFlag;
		data.timegranularity = timegranularity;
		data.spatialgranularityFlag = spatialgranularityFlag;
		data.geoshapePath = geoshapePath;
		data.outflag = outflag;
		data.outFlagAndPath = outPath;
		data.taskId = taskId;
		return data;
	}

	/**
	 * 生成任务id
	 * 
	 * @param modelminId
	 * @param modelId
	 * @param userId
	 * @param outPath
	 * @param endDateTime
	 * @param startDateTime
	 * @param dataClass
	 * @param dataSourse
	 * @return
	 */
	private static synchronized String getTaskId(String userId, String modelId, String modelminId, String dataSourse, String dataClass, String startDateTime, String endDateTime) {
		Date date = new Date();
		String returnStr = userId + "_" + modelId + "_" + modelminId + "_" + dataSourse + "_" + dataClass + "_" + startDateTime + "_" + endDateTime + "_" + date.getTime();
		return returnStr;
	}
}
